package com.java2007.hotel.controller.front;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.controller.BaseServlet;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Admin;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.front.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;

/**
 * 处理用户的请求
 *
 * @author csl
 * @version V1.0
 * @date 2021/01/25
 **/
@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    private UserService userService = (UserService) BeanFactory.getBean(Constant.USER_SERVICE_IMPL);
    /**
     * 增加一个用户
     *
     * @return
     */
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        String userName = req.getParameter("userName").trim();
        String userPwd = req.getParameter("userPwd").trim();
        String userPhone = req.getParameter("userPhone").trim();
        String userEmail = req.getParameter("userEmail").trim();
        String tempUserRole = req.getParameter("isVip").trim();
        Byte adminRole = Byte.valueOf(tempUserRole);
        Date date = new Date();
        User user = new User(null, userName, userPwd, adminRole, date, userPhone, userEmail,null);

        System.out.println(user);
        int i = 0;
        try {
            i = userService.add(user);
            System.out.println(i);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器出错了%"+"userRegister.jsp";
        }
        if (i > 0){
            return "redirect:"+req.getContextPath()+"/frontindex.jsp";
        }else {
            return "toIndex";
        }
    }

    /**
     * 客户登录
     * @param req
     * @param resp
     * @return
     */
    public String login(HttpServletRequest req, HttpServletResponse resp){
        String userName = req.getParameter("loginUserName").trim();
        String userPwd = req.getParameter("loginUserPwd").trim();
        String verificationCode = req.getParameter("verificationCode").trim();
        HttpSession session = req.getSession();
        String validateCode = ((String) session.getAttribute("validateCodeFront")).trim();

        if (validateCode.equalsIgnoreCase(verificationCode)){
            User user = null;
            try {
                user = userService.selectByName(userName, userPwd);
                System.out.println("user=======>"+user);

            } catch (Exception e) {
                return "back:" + "用户或者密码错误%"+"frontindex.jsp";
            }
            if (null != user){
                session.setAttribute("user",user);
                return "redirect:"+req.getContextPath()+"/front/tableServlet?method=selectAll";
            }else {
                return "back:" + "用户或者密码错误%"+"frontindex.jsp";
            }
        }else {
            return "back:" + "验证码错误%"+"frontindex.jsp";
        }
    }

}
